﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using PowerCreator.LMS.ViewQuery;

public partial class sysadmin_CountManager_PaperReadCount : PowerCreator.LMS.Web.Base.BasePage
{
    protected PowerCreator.LMS.Web.User.User opUser = new PowerCreator.LMS.Web.User.User();
     string PaperReadCountSearch = "select PaperID, PaperName,count(*)as SumCount,sum(UPHistoryReadState)as ReadCount from UserPaperHistory "
       + "inner join Paper  on Paper.PaperID=UserPaperHistory.FKUPHistoryPaperID where 1=1 {0} group by PaperName,PaperID";
     string PaperReadCountSearchCount = "select FKUPHistoryPaperID from UserPaperHistory "
       + "inner join Paper  on Paper.PaperID=UserPaperHistory.FKUPHistoryPaperID where 1=1 {0}";
    ViewQuery objViewQuery = new ViewQuery();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();

            string search = GetSearch();
            //PowerCreator.LMS.Common.Script.Script.Alert(this,"msg", search);
            BinderPager(search);
            BinderRPT(search, 1);
        }
    }

    protected override void InitPage()
    {
        lbl_InfoTitle.Text = GetLanageValueByKey("SysCoursePaperReadCount", PowerCreator.LMS.Language.LanagetItemType.Page);// "试卷批阅统计";GetLanageValueByKey("SysOnlineUserManager_TitleInfo", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_PaperName.Text = GetLanageValueByKey("lab_AddPaperInfo_PaperName", PowerCreator.LMS.Language.LanagetItemType.Page);
       
        btn_Search.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);


        base.InitPage();
    }

    private string GetSearch()
    {
         string Search=string.Empty;
        if(txt_PaperName.Text.Trim()!="")
        {
            string arg= " and PaperName like'%"+txt_PaperName.Text.Trim()+"%'";
            Search = string.Format(PaperReadCountSearch, arg);
            PaperReadCountSearchCount = string.Format(PaperReadCountSearchCount, arg);
        }
        else
        {
            Search = string.Format(PaperReadCountSearch, "");
            PaperReadCountSearchCount = string.Format(PaperReadCountSearchCount, "");
        }
        
        return Search;
        
        
    }

    private void BinderPager(string search)
    {
        AspNetPager1.RecordCount = objViewQuery.Count(search);//PaperReadCountSearchCount
        AspNetPager1.PageSize = LMSPageSize;
    }

    private void BinderRPT(string search, int pageIndex)
    {
        DataTable dt = new DataTable();

        dt = objViewQuery.Query(search, LMSPageSize, pageIndex);
        dt.Columns.Add("UnReadCount");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            dt.Rows[i]["UnReadCount"] = int.Parse(dt.Rows[i]["SumCount"].ToString()) - int.Parse(dt.Rows[i]["ReadCount"].ToString());
        }
        rpt_UserOnline.DataSource = dt;
        rpt_UserOnline.DataBind();
    }

    protected void rpt_UserOnline_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        //if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        //{
        //    LinkButton lbtn = (LinkButton)e.Item.FindControl("lnkbtn_del");
        //    lbtn.Text = GetLanageValueByKey("btn_ManagerQuestion_Delete", PowerCreator.LMS.Language.LanagetItemType.Page);
        //    lbtn.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");
        //}
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, 1);
    }
    protected void btn_CurrentSearch_Click(object sender, EventArgs e)
    {

    }
    protected void rpt_UserOnline_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        //if (e.CommandName == "ldel")
        //{

        //    if (opUser.DelOnlineUser(int.Parse(e.CommandArgument.ToString())))
        //    {
        //        string search = GetSearch();
        //        BinderPager(search);
        //        BinderRPT(search, AspNetPager1.CurrentPageIndex);
        //    }
        //}
    }
}
